#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>

struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
    
};

struct TreeNode* BuyNode(int x)
{
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    if (node == NULL)
    {
        perror("malloc fail");
        return NULL;
    }

    node->val = x;
    node->left = NULL;
    node->right = NULL;

    return node;
}

bool isUnivalTree(struct TreeNode* root) {
    if (root->left == NULL && root->right == NULL) {
        return true;
    }
    if ((root->left != NULL && root->right == NULL) ||
        (root->left == NULL && root->right != NULL)) {
        if (root->left == NULL) {
            if ((root->right->val) == (root->val)) {
                return true;
            }
        }
        else {
            return false;
        }

        if (root->right == NULL) {
            if ((root->left->val) == (root->val)) {
                return true;
            }
        }
        else {
            return false;
        }
    }
    if ((root->left->val) != (root->right->val)) {
        return false;
    }
    bool ret = isUnivalTree(root->left);
    bool rec = isUnivalTree(root->right);
    if (rec == false || ret == false) {
        return false;
    }
    return true;
}

int main() {
    struct TreeNode* node1 = BuyNode(1);
    struct TreeNode* node2 = BuyNode(1);

    node1->left = node2;
    isUnivalTree(node1);
    return 0;
}